package com.qf.common.core.utils;

import com.qf.common.core.common.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2025/11/6
 * @desc
 */
@Component
public class TokenUtils {

    @Autowired
    private JwtUtils jwtUtils;

    public Integer getUserIdFromToken(){
        ServletRequestAttributes requestAttributes =(ServletRequestAttributes)  RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader("token");
        Claims claims = jwtUtils.parseToken(token);
        Integer uid = (Integer) claims.get("uid");
        return uid;
    }

}
